package com.example.myBlogTest.controller;

import com.example.myBlogTest.common.lang.Result;
import com.example.myBlogTest.dto.UserRequest;
import com.example.myBlogTest.dto.UserResponse;
import com.example.myBlogTest.entity.User;
import com.example.myBlogTest.exception.SaveObjectException;
import com.example.myBlogTest.service.UserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/user")
public class UserController {

    private UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping
    public Result saveUser(@Validated @RequestBody UserRequest userRequest) {
        try {
            UserResponse userResponse = userService.saveUser(userRequest);
            return Result.succ(userResponse);
        } catch (SaveObjectException e) {
            return Result.fail(e.getMessage());
        }
    }
}
